package cc.rengu.redp.bizimpl.igas.controller;


import cc.rengu.redp.bizimpl.bean.LabelValueBean;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.enums.SettleAccTypeEnum;
import cc.rengu.redp.bizimpl.igas.domain.*;
import cc.rengu.redp.bizimpl.igas.domain.PSecPlanInfo;
import cc.rengu.redp.bizimpl.igas.service.*;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.*;

/**
 * @author 刘玉亮.LL
 */
@Slf4j
@Api(tags = "安全计划管理")
@RestController
@RequestMapping("/admin/igas/pSecPlanInfo")
public class PSecPlanInfoController extends BaseController {
    @Autowired
    private PSecPlanInfoService pSecPlanInfoService;
    @Autowired
    private PKeyInfoService pKeyInfoService;
    @Autowired
    private PCertInfoService pCertInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, PSecPlanInfo entity) {
        return getDataTable(this.pSecPlanInfoService.findByPage(request, entity));
    }

    @GetMapping(value = "/editFromChnl")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "操作类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "secPlanType", value = "安全计划类型", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "secPlanId", value = "安全计划标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public Map<String, Object> editFromChnl(@NotBlank(message = "{required}") String optFlag, @NotBlank(message = "{required}")String secPlanType,
                                            @NotBlank(message = "{required}")String secPlanId, @NotBlank(message = "{required}")String instId) throws RedpException {
        HashMap<String, Object> result = new HashMap<>();
        PSecPlanInfo entity;
        PKeyInfo pKeyInfo;
        PCertInfo pCertInfo;
        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(optFlag)) {
            entity = new PSecPlanInfo();
            entity.setInstId(instId);
            entity.setSecPlanId(secPlanId);
            entity.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
            entity.setKeyType("C");
            entity.setSecPlanStatus(IgasYesOrNoEnum.Yes.getType());
            pCertInfo = new PCertInfo();
            pCertInfo.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
            pCertInfo.setCertStatus(IgasYesOrNoEnum.Yes.getType());
            pKeyInfo = new PKeyInfo();
            pKeyInfo.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
            pKeyInfo.setKeyStatus(IgasYesOrNoEnum.Yes.getType());
            result.put("pKeyInfo", pKeyInfo);
            result.put("pCertInfo", pCertInfo);
        } else {
            if (Common.isBlank(secPlanType)) {
                 log.error("请求参数[安全计划类型]不能为空！");
                throw new RedpException("请求参数[安全计划类型]不能为空！");
            }
            entity = pSecPlanInfoService.findOne(instId, secPlanId, secPlanType);
            if (Common.isNotBlank(entity)) {
                entity.setKeyType(entity.getSecPlanType().substring(0, 1));
                entity.setKeyOprType(entity.getSecPlanType().substring(1, 2));
                entity.setKeyUsage(entity.getSecPlanType().substring(2, 4));
                if ("C".equals(entity.getKeyType())) {
                    pCertInfo = pCertInfoService.getById(entity.getInstId() + entity.getSecPlanId() + entity.getSecPlanType());
                    if (Common.isBlank(pCertInfo)) {
                        pCertInfo = new PCertInfo();
                        pCertInfo.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        pCertInfo.setCertStatus(IgasYesOrNoEnum.Yes.getType());
                    }
                    result.put("pCertInfo", pCertInfo);
                } else if ("K".equals(entity.getKeyType())) {
                    pKeyInfo = pKeyInfoService.getById(entity.getInstId() + entity.getSecPlanId() + entity.getSecPlanType());
                    if (Common.isBlank(pKeyInfo)) {
                        pKeyInfo = new PKeyInfo();
                        pKeyInfo.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                        pKeyInfo.setKeyStatus(IgasYesOrNoEnum.Yes.getType());
                    }
                    result.put("pKeyInfo", pKeyInfo);
                }
            } else {
                entity = new PSecPlanInfo();
                entity.setInstId(instId);
                entity.setSecPlanId(secPlanId);
                entity.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                entity.setKeyType("C");
                entity.setSecPlanStatus(IgasYesOrNoEnum.Yes.getType());
                pCertInfo = new PCertInfo();
                pCertInfo.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                pCertInfo.setCertStatus(IgasYesOrNoEnum.Yes.getType());
                pKeyInfo = new PKeyInfo();
                pKeyInfo.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
                pKeyInfo.setKeyStatus(IgasYesOrNoEnum.Yes.getType());
                result.put("pKeyInfo", pKeyInfo);
                result.put("pCertInfo", pCertInfo);
            }
        }
        entity.setOptFlag(optFlag);
        result.put("entity", entity);
        result.put("optFlag", optFlag);
        return result;
    }

    @PostMapping(value = "/editFromChnl")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "dstChannelId", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public void editFromChnl(@ModelAttribute("pSecPlanInfo") PSecPlanInfo entity,
                             @ModelAttribute("pKeyInfo") PKeyInfo pKeyInfo,
                             @ModelAttribute("pCertInfo") PCertInfo pCertInfo) throws RedpException {
        pSecPlanInfoService.saveData(entity, pKeyInfo, pCertInfo);
    }

    @PostMapping(value = "/changeStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "dstChannelId", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public void changeStatus(PSecPlanInfo entity) throws RedpException {
        LambdaQueryWrapper<PSecPlanInfo> wraper = new LambdaQueryWrapper<>();
        wraper.eq(PSecPlanInfo::getInstId, entity.getInstId());
        wraper.eq(PSecPlanInfo::getSecPlanId, entity.getSecPlanId());
        wraper.eq(PSecPlanInfo::getSecPlanType, entity.getSecPlanType());
        PSecPlanInfo db = pSecPlanInfoService.getOne(wraper);
        if (Common.isBlank(db)) {
            log.error("根据唯一索引没有找到指定数据！");
            throw new RedpException("没有找到指定数据！");
        }
        if (IgasYesOrNoEnum.Yes.getType().equals(entity.getSecPlanStatus())
                || IgasYesOrNoEnum.No.getType().equals(entity.getSecPlanStatus())) {
            pSecPlanInfoService.saveData(entity, null, null);
        }
    }
}
